package com.example.controller.before;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.entity.Pyq;
import com.example.entity.PyqComment;
import com.example.entity.User;
import com.example.entity.vo.PyqCommentVo;
import com.example.entity.vo.PyqVo;
import com.example.service.PyqCommentService;
import com.example.service.PyqService;
import com.example.service.UserService;
import com.example.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "小程序圈子") //swagger的注释
@CrossOrigin
@RequestMapping("/PyqBefore")
@RestController
public class PyqControllerBefore {

    @Autowired
    private PyqService pyqService;

    @Autowired
    private PyqCommentService pyqCommentService;

    @Autowired
    private UserService userService;

    @ApiOperation(value = "查看圈子")
    @GetMapping(value = "getPyq/{current}/{limit}")
    public R getPyq(
            @ApiParam(name = "current", value = "当前页", required = true) @PathVariable long current,
            @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable long limit
    ) {
        Page<Pyq> page = new Page<>(current, limit);
        pyqService.page(page, null);
        //获取分页后数据
        long total = page.getTotal();
        List<Pyq> list = page.getRecords();
        List<PyqVo> list2 = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            PyqVo pyqVo = new PyqVo();
            pyqVo.setPyqId(list.get(i).getPyqId());
            pyqVo.setPyqContent(list.get(i).getPyqContent());
            pyqVo.setPyqPicture(list.get(i).getPyqPicture());
            pyqVo.setGmtCreate(list.get(i).getGmtCreate());
            String userId = list.get(i).getUserId();
            User user = userService.getById(userId);
            pyqVo.setUser(user);
            list2.add(pyqVo);
        }

        return R.ok().data("total", total).data("list", list2);
    }

    @ApiOperation(value = "查看圈子评论")
    @GetMapping(value = "getPyqComment/{pyqId}")
    public R getPyqComment(@PathVariable String pyqId) {
        QueryWrapper<PyqComment> wrapper = new QueryWrapper<>();
        wrapper.eq("pyq_id", pyqId);
        wrapper.orderByAsc("gmt_create");
        List<PyqComment> list = pyqCommentService.list(wrapper);
        List<PyqCommentVo> list2 = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            PyqCommentVo pyqCommentVo = new PyqCommentVo();
            pyqCommentVo.setCommentId(list.get(i).getCommentId());
            pyqCommentVo.setPyqId(list.get(i).getPyqId());
            pyqCommentVo.setComment(list.get(i).getComment());
            pyqCommentVo.setGmtCreate(list.get(i).getGmtCreate());
            User reviewTo = userService.getById(list.get(i).getReviewTo());
            User userSend = userService.getById(list.get(i).getUserId());
            pyqCommentVo.setReviewTo(reviewTo);
            pyqCommentVo.setUserSend(userSend);
            list2.add(pyqCommentVo);
        }
        return R.ok().data("list", list2);
    }

    @ApiOperation(value = "添加圈子")
    @PostMapping(value = "addPyq")
    public R addPyq(@RequestBody Pyq pyq) {
        boolean b = pyqService.save(pyq);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation(value = "添加圈子评论")
    @PostMapping(value = "addPyqComment")
    public R addPyqComment(@RequestBody PyqComment pyqComment) {//review_to,comment,pyq_id,user_id(评论者id)
        boolean b = pyqCommentService.save(pyqComment);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }

}
